﻿namespace ImapComponent.Net.Mail.Mime
{
    using System.Collections.Generic;
    using System.Net.Mime;

    /// <summary>
    /// Represents the mime multipart.
    /// </summary>
    public class MimeMultipart : MimePart
    {
        #region Fields

        private readonly MimePartCollection children;

        #endregion // Fields

        #region Constructors

        /// <summary>
        /// Initializes a new instance of the MimeMultipart class.
        /// </summary>
        /// <param name="parent">A mime part parent.</param>
        /// <param name="contentData">A data content.</param>
        /// <param name="contentType">A content type.</param>
        /// <param name="contentId">A content id.</param>
        /// <param name="transferEncoding">A transfer encoding.</param>
        public MimeMultipart(MimePart parent, byte[] contentData, ContentType contentType, string contentId, TransferEncoding transferEncoding)
            : base(parent, contentData, contentType, contentId, transferEncoding)
        {
            this.children = new MimePartCollection();
        }

        #endregion // Constructors

        #region Properties

        /// <summary>
        /// Gets the children mime part collection.
        /// </summary>
        public MimePartCollection Children
        {
            get
            {
                return this.children;
            }
        }

        #endregion // Properties

        #region Methods

        /// <summary>
        /// Adds the plain bodies for the specified collection.
        /// </summary>
        /// <param name="plainBodies">The collection of the plain bodies.</param>
        internal void SetPlainText(List<MimeSinglepart> plainBodies)
        {
            foreach (MimePart mimePart in this.children)
            {
                if (mimePart is MimeSinglepart)
                {
                    MimeSinglepart mimeSinglePart = mimePart as MimeSinglepart;

                    if (mimeSinglePart.ContentType.MediaType.ToLower() == "text/plain")
                    {
                        plainBodies.Add(mimeSinglePart);
                    }
                }
                else if (mimePart is MimeMultipart)
                {
                    MimeMultipart mimeMultipart = mimePart as MimeMultipart;

                    mimeMultipart.SetPlainText(plainBodies);
                }
            }
        }

        /// <summary>
        /// Adds the html bodies for the specified collection.
        /// </summary>
        /// <param name="plainBodies">The collection of the html bodies.</param>
        internal void SetHtmlText(List<MimeSinglepart> htmlBodies)
        {
            foreach (MimePart mimePart in this.children)
            {
                if (mimePart is MimeSinglepart)
                {
                    MimeSinglepart mimeSinglePart = mimePart as MimeSinglepart;

                    if (mimeSinglePart.ContentType.MediaType.ToLower() == "text/html")
                    {
                        htmlBodies.Add(mimeSinglePart);
                    }
                }
                else if (mimePart is MimeMultipart)
                {
                    MimeMultipart mimeMultipart = mimePart as MimeMultipart;

                    mimeMultipart.SetHtmlText(htmlBodies);
                }
            }
        }

        /// <summary>
        /// Sets the attachments for the specified collection.
        /// </summary>
        /// <param name="attachments">The collection of the email message attachments.</param>
        internal void SetAttachments(MimeAttachmentCollection attachments)
        {
            foreach (MimePart mimePart in this.children)
            {
                if (mimePart is MimeAttachment)
                {
                    MimeAttachment attachment = mimePart as MimeAttachment;

                    attachments.Add(attachment);
                }
                else if (mimePart is MimeMultipart)
                {
                    MimeMultipart mimeMultipart = mimePart as MimeMultipart;

                    mimeMultipart.SetAttachments(attachments);
                }
            }
        }

        #endregion // Methods
    }
}
